package com.javafactroy.kunlong.domain.deptemp.repository;

import com.javafactroy.kunlong.data.dto.VDeptApiDto;
import com.javafactroy.kunlong.data.service.VDeptApiService;
import com.javafactroy.kunlong.domain.deptemp.agg.DeptTreeAgg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
public class DeptTreeAggRepository {
    @Autowired
    VDeptApiService apiService;

    public DeptTreeAgg load() {
        List<VDeptApiDto> list = apiService.selectAll();
        DeptTreeAgg agg = new DeptTreeAgg();
        List<VDeptApiDto> root = list.stream().filter(i -> i.getParentId().equals(0L)).collect(Collectors.toList());
        root.forEach(i -> loadSub(i,list));


        return agg.setDeptApiDtos(root);

    }

    public void loadSub(VDeptApiDto dto, List<VDeptApiDto> list) {

        List<VDeptApiDto> child = list.stream().filter(i -> i.getParentId().equals(dto.getDeptId())).collect(Collectors.toList());
        dto.setChild(child);

        child.forEach(i->loadSub(i,list));
    }
}
